package hot100;

import java.util.HashMap;

public class Solution581 {
    public int findUnsortedSubarray(int[] nums) {
        int len = nums.length;
        int max = nums[0];
        int min = nums[len - 1];
        int start = 0;
        int end = -1;
        for (int i = 1; i < len; i++) {
            if (nums[i] >= max) max = nums[i];
            else end = i;
            if (nums[len - 1 - i] <= min) min = nums[len - 1 - i];
            else start = len - 1 - i;
        }
        return end - start + 1;
    }
}
